package com.hy.common.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hy.common.consts.CommonConst;
import com.hy.common.utils.CommonUtils;
import com.hy.common.utils.MyObjectUtils;

import com.hy.entity.pojo.HyPlace;
import com.hy.entity.vo.HyPlaceReadVO;

import com.hy.mapper.HyDepartmentMapper;
import com.hy.mapper.HyPlaceMapper;
import com.hy.mapper.HyUnitMapper;
import com.hy.service.HyPlaceService;

import org.springframework.util.ObjectUtils;

import java.util.*;

public class HyPropertyPlaceImport extends AbstractExcelImport<HyPlaceReadVO, HyPlace>{


    private HyDepartmentMapper departmentMapper;
    private Integer runTimes=0;
    private HyUnitMapper unitMapper;
    private HyPlaceService placeService;
    private HyPlaceMapper placeMapper;

    public HyPropertyPlaceImport(HyDepartmentMapper departmentMapper, HyUnitMapper unitMapper, HyPlaceService placeService, HyPlaceMapper placeMapper) {
        this.departmentMapper = departmentMapper;
        this.unitMapper = unitMapper;
        this.placeService = placeService;
        this.placeMapper = placeMapper;
    }

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        boolean valid = validateHeader(headMap);
        if (!valid) {

            throw new RuntimeException("表头校验失败，请检查Excel文件的列名");
        }
    }
    private boolean validateHeader(Map<Integer, String> headMap) {

        Set<String> expectedHeaders=new HashSet<>();
        Collections.addAll(expectedHeaders,"区域名称","所属部门","所属单位","区域上级","占地面积(单位㎡)");
        Set<String> actualHeaders = new HashSet<>(headMap.values());
        return actualHeaders.containsAll(expectedHeaders);
        // 检查表头是否与预期的列名一致
    }
    @Override
    public void invoke(HyPlaceReadVO object, AnalysisContext context) {
       try{

           if (MyObjectUtils.areAllFieldsNull(object)){
               return;
           }
           HyPlace place=new HyPlace();

           Integer unitId = unitMapper.findUnitIdByUnitName(object.getUnitName());
           HyPlace hyPlace1 = placeMapper.selectOne(new LambdaQueryWrapper<HyPlace>().and(wapper ->
                   wapper.eq(HyPlace::getPlaceName, object.getPlaceName())
                           .eq(HyPlace::getUnitId, unitId)

           ));
           if (!ObjectUtils.isEmpty(hyPlace1)){
               throw new ExcelAnalysisException("区域名称重复 ：第："+(context.getCurrentRowNum()+1)+"行");
           }
           Integer deptId = MyObjectUtils.isNotBlank(object.getDepartmentName())?departmentMapper.findDeptIDByUnitIDAndDeptName(unitId, object.getDepartmentName()): CommonConst.DEFAULT_ID;
           Integer mgrId="第一级".equals(object.getMgrName())?0:placeMapper.findplaceIdByUnitIdAndPlaceName(unitId,object.getMgrName());
            place.setArea(object.getArea());
            place.setUnitId(unitId);
            place.setDel(1);
            place.setOwnerDeptId(deptId);
            place.setPlaceMgr(mgrId);
            place.setCreateDate(CommonUtils.getDate());
           place.setPlaceName(object.getPlaceName());
            if (mgrId.equals(0)){
                HyPlace hyPlace = placeMapper.selectOne(new LambdaQueryWrapper<HyPlace>().and(wapper ->
                        wapper.eq(HyPlace::getPlaceName, object.getPlaceName()))
                        .eq(HyPlace::getUnitId,unitId));
                if (hyPlace!=null){
                    throw  new ExcelAnalysisException("第一级菜单跟数据库的有重复,第："+(context.getCurrentRowNum()+1)+"行");
                }else {
                    placeMapper.insert(place);

                }
                return;
            }
            dataList.add(place);

       }catch (Exception e){
           throw new ExcelAnalysisException(e.getMessage());
       }
    }

    @Override
    protected void doSaveData(List<HyPlace> data) {
        placeService.saveStuListWithBatch(data);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
       doSaveData(dataList);
    }
}
